package com.sqs.controller;

import com.sqs.constant.ResultConstant;
import com.sqs.constant.StatusConstant;
import com.sqs.context.BaseContext;
import com.sqs.dto.EditPasswordDTO;
import com.sqs.dto.UserEnrollDTO;
import com.sqs.dto.UserLoginDTO;
import com.sqs.dto.UserUpdateDTO;
import com.sqs.entity.User;
import com.sqs.result.Result;
import com.sqs.service.IUserService;
import com.sqs.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理controller
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户注册
     * @param userEnrollDTO
     * @return
     * */
    @PostMapping("/enroll")
    public Result saveNewUser(@RequestBody UserEnrollDTO userEnrollDTO){
        log.info("新增用户：{}", userEnrollDTO);
        iUserService.insert(userEnrollDTO);
        return Result.success(ResultConstant.ENROLL_SUCCESS);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     * */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}", userLoginDTO);

        UserLoginVO userLoginVO = iUserService.login(userLoginDTO);

        return Result.success(ResultConstant.LOGIN_SUCCESS, userLoginVO);
    }

//    /**
//     * 更改用户启用禁用状态
//     * @param id
//     * @param status
//     * @return
//     * */
//    @PostMapping("/status/{status}")
//    public Result stopOrStart(Long id, @PathVariable Integer status){
//        log.info("将id为{}的用户的状态更改为{}", id,status == 1 ? StatusConstant.ENABLE_STRING : StatusConstant.UNABLE_STRING);
//        iUserService.stopOrStart(id, status);
//        return Result.success(ResultConstant.UPDATE_SUCCESS);
//    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     * */
    @GetMapping
    public Result<User> getById(Long id){
        log.info("根据id查询用户,id:{}", id);
        User user = iUserService.getById(id);
        return Result.success(ResultConstant.SEARCH_SUCCESS, user);
    }

    /**
     * 更改用户密码
     * @param editPasswordDTO
     * @return
     * */
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody EditPasswordDTO editPasswordDTO){
        log.info("更改用户密码");
        iUserService.editPassword(editPasswordDTO);
        return Result.success(ResultConstant.UPDATE_SUCCESS);
    }

    /**
     * 修改用户信息
     * @param userUpdateDTO
     * @return
     * */
    @PutMapping
    public Result update(@RequestBody UserUpdateDTO userUpdateDTO){
        log.info("修改用户信息，{}", userUpdateDTO);
        iUserService.update(userUpdateDTO);
        return Result.success(ResultConstant.UPDATE_SUCCESS);
    }

    /**
     * 退出登录
     * @return
     * */
    @PostMapping("/loginout")
    public Result loginOut(){
        log.info("退出登录:{}", BaseContext.getCurrentId());
        iUserService.loginOut();
        return Result.success(ResultConstant.LOGIN_OUT_SUCCESS);
    }

}
